//
//  PilotPickerViewController.m
//  TabPilotEdit
//
//  Created by Scott Guyer on 7/19/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "EVEApi.h"
#import "EditPilotViewController.h"
#import "PilotPickerViewController.h"
#import "defs.h"


@implementation PilotPickerViewController

@synthesize headerView, activityView, editPilotVC, selectedCell = lastSelectedCell;



- (id) init
{
	NSArray* nibObjects = [[NSBundle mainBundle] loadNibNamed:@"PilotPickerView" owner:self options:nil];
	if ( nibObjects == nil )
	{
		NSLog(@"%s unable to load nib", __FUNCTION__);
		return nil;
	}
	
	charList = nil;
	
	[super initWithStyle:UITableViewStyleGrouped];
	lastSelectedCell = nil;	
	
	[activityView setHidesWhenStopped:YES];
	[activityView stopAnimating];

	return self;
}


- (void) setCharactersFromDictionary:(NSDictionary*)dict 
{
	if ( dict == nil )
	{
		if ( charList != nil )
		{
			[charList release];
			charList = nil;
		}
		return;
	}
	
	if ( charList != nil )
	{
		[charList release];		
	}
	
	charList = [[NSMutableArray alloc] initWithCapacity:5];
	
	for ( NSString* p in dict )
	{
		[charList addObject:p];
	}

	[self.tableView reloadData];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


/*
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

	return [NSString stringWithString:@"Select your pilot..."];
}
 */


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return 61.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
	return headerView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
	if ( charList != nil )
		return [charList count];
	else
		return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
	static NSString *MyIdentifier = @"pilotNameCell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	if (cell == nil) {
		CGRect zero = CGRectMake(0,0,0,0);
		cell = [[[UITableViewCell alloc] initWithFrame:zero reuseIdentifier:MyIdentifier] autorelease];
	}

	[cell setText:[charList objectAtIndex:indexPath.row]];
	[cell setAccessoryType:UITableViewCellAccessoryNone];
	
	return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
	// Disable the old checkmark accessory
	if ( lastSelectedCell != nil )
	{
		[lastSelectedCell setAccessoryType:UITableViewCellAccessoryNone];
		lastSelectedCell = nil;
	}
	
	UITableViewCell* c = [self.tableView cellForRowAtIndexPath:indexPath];
	[c setSelected:NO];
	[c setAccessoryType:UITableViewCellAccessoryCheckmark];
	lastSelectedCell = c;
}



- (IBAction) refreshAction:(id) sender
{
	if ( editPilotVC != nil )
	{
		[editPilotVC loadCharacters];
		[activityView startAnimating];
	}
}



 
- (void)dealloc 
{
	NSLog(@"%s 0x%x", __FUNCTION__, self);
	
	WIPE( lastSelectedCell)
	WIPE( headerView)
	WIPE( activityView)
	WIPE( charList)
	WIPE( editPilotVC)	
	
	[super dealloc];
}


- (void)viewDidLoad {
	[super viewDidLoad];
}


- (void)viewWillAppear:(BOOL)animated {
	NSLog(@"%s", __FUNCTION__);
	[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
	NSLog(@"%s", __FUNCTION__);
	[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
	NSLog(@"%s", __FUNCTION__);
}

- (void)viewDidDisappear:(BOOL)animated {
	NSLog(@"%s", __FUNCTION__);
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
}


@end

